{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from pprint import pprint as p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(3)\n",
    "b = np.arange(4)\n",
    "p(a)\n",
    "p(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 以下语句会出错\n",
    "# a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[:,None] + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[:,None].shape, b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 广播兼容的条件\n",
    "# 1. 从后面算起，对应轴的长度相等\n",
    "# 2. 从后面算起，对应轴的长度任何一个是1\n",
    "a = np.ones((2,3,4))\n",
    "b = np.ones((3,4))\n",
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 两个阵列形状兼容\n",
    "a.shape, b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把参与计算的阵列的轴倒过来对比，长度相等，或者其中一个长度为一，就兼容\n",
    "a = np.ones((2,1,4))\n",
    "b = np.ones((2,1))\n",
    "c = np.ones((1,4))\n",
    "a + b + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 广播的行为\n",
    "a = np.arange(6).reshape(2,3)\n",
    "b = np.arange(3)\n",
    "p(a)\n",
    "p(b)\n",
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对比轴\n",
    "a.shape, b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# b 的形状先变成 (1,3)\n",
    "p(b)\n",
    "b2 = b[None,:]\n",
    "p(b2)\n",
    "b.shape, b2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 然后拉伸短的轴，使之与长的轴长度相同\n",
    "a.shape, b2.shape\n",
    "b3 = np.array([b,b])\n",
    "p(b2)\n",
    "p(b3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a1, a2 = np.broadcast_arrays(a,b)\n",
    "p(a)\n",
    "p(b)\n",
    "print('-' * 30)\n",
    "p(a1)\n",
    "p(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(2)\n",
    "b = np.arange(3)\n",
    "p(a[:,None] + b)\n",
    "p(a + b[:,None])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
